<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="conent">
			<navigator :url="'/pages/preview/preview?id='+ item._id" class="item" v-for="item in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadingLayout" v-if="classList.length || noData">
			<uni-load-more :status="noData? 'noMore' : 'loading'"></uni-load-more>
		</view>
		
		<view class="safe-area-inset-buttom"></view>
	</view>
</template>

<script setup>
import {ref} from 'vue'
import {onLoad,onUnload,onShareAppMessage,onShareTimeline,onReachBottom,onPullDownRefresh} from "@dcloudio/uni-app"
import {apiGetClassList,apiGetHistoryList} from "@/apis/index.js"
import {gotoHome} from "@/utils/common.js"

const classList = ref([]);
const noData = ref(false)
let pageName;

// 定义data参数
const queryParams ={
	pageNum:1,
	pageSize:12
}

onLoad((e)=>{
	// 如果没有id就返回首页执行方法
	// if(!id) gotoHome()
	const {id =null,name =null,type=null} = e;
	if(type) queryParams.type = type;
	if(id) queryParams.classid = id;
	
	pageName = name
	// 修改导航标题
	uni.setNavigationBarTitle({
		title:name
	})
	// 执行获取分类列表方法
		getClassList()
})

// 获取分类列表网络数据
const getClassList = async()=>{
	let res;
	if(queryParams.classid) res = await apiGetClassList(queryParams);
	if(queryParams.type) res = await apiGetHistoryList(queryParams);

	
	classList.value =[...classList.value ,...res.data.data]
	if(queryParams.pageSize > res.data.data.length)noData.value = true;
	uni.setStorageSync("storgClassList",classList.value)
	console.log(classList.value);
	uni.stopPullDownRefresh()
}

// 触底加载更多
onReachBottom(()=>{
	if(noData.value)return;
	queryParams.pageNum++
	getClassList()
})

	// 分享给好友
		onShareAppMessage((e)=>{
			console.log(e);
			return{
				title:'壁纸大全-' + pageName,
				path:'/pages/classlist/classlist?id='+ queryParams.classid+"&name=" +pageName
			}
		})
	
	// 分享朋友圈
	onShareTimeline(()=>{
		return{
			title:'壁纸大全-' + pageName,
			query:"id=" + queryParams.classid + "&name=" +pageName,
			imageUrl:'/static/logo.jpg'
		}
	})
	
	// 离开页面之后把缓存清除掉
	onUnload(()=>{
		uni.removeStorageSync("storgClassList")
	})
	
	onPullDownRefresh(()=>{
		classList.value = [];
		getClassList()
	})
</script>

<style lang="scss" scoped>
.classlist{
	.conent{
	display: grid;
	grid-template-columns: repeat(3,1fr);
	gap: 5rpx;
	padding: 5rpx;
	.item{
		height: 440rpx;
		image{
			width: 100%;
			height: 100%;
			display: block;
		}
	}}
}
</style>
